package com.qiaomeng.flutter.flutter_jpush_vip;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.ups.JPushUPSManager;
import cn.jpush.android.ups.TokenResult;
import cn.jpush.android.ups.UPSRegisterCallBack;

public class Handler {
    private Context context;

    public Handler(Context context) {
        this.context = context;
    }

    public void init(UPSRegisterCallBack callBack) {
        JPushUPSManager.registerToken(context, getAppkey(), null, null, callBack);
    }

    private String getAppkey() {
        String value = "";
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
            value = applicationInfo.metaData.getString("JPUSH_APPKEY");
        } catch (Exception e) {
            value = "";
        }
        return value;
    }

    public void debug() {
        JPushInterface.setDebugMode(true);
    }

    /**
     * 获取注册ID
     *
     * @return
     */
    public String getRegistrationID() {
        return JPushInterface.getRegistrationID(context);
    }

    /**
     * 清除通知
     */
    public void clearAllNotifications() {
        JPushInterface.clearAllNotifications(context);
    }


    /**
     * 点击通知时获取消息
     *
     * @param intent
     * @return
     */
    public String getPushMessage(Intent intent) {
        String data = null;
        //获取华为平台附带的jpush信息
        if (intent.getData() != null) {
            data = intent.getData().toString();
        }
        //获取fcm、oppo、vivo、华硕、小米平台附带的jpush信息
        if (TextUtils.isEmpty(data) && intent.getExtras() != null) {
            data = intent.getExtras().getString("JMessageExtra");
        }
        return data;
    }


}


